﻿using System;
using ImGuiNET;
using Painter3D.RenderSys;
using System.Numerics;

namespace Painter3D.UI
{
    class UI
    {
        static TextureGrid icons;

        public static void Init()
        {
            icons = new TextureGrid(new System.Collections.Generic.Dictionary<string, string>() {
                {"pickIcon",@"F:\Painter3D\Resource\Texture\Icons\pick.png" },
                {"moveIcon",@"F:\Painter3D\Resource\Texture\Icons\move.png" },
                {"trackRotateIcon",@"F:\Painter3D\Resource\Texture\Icons\trackRotate.png" },
                {"zoomIcon",@"F:\Painter3D\Resource\Texture\Icons\zoom.png" },
                {"fitIcon",@"F:\Painter3D\Resource\Texture\Icons\fit.png" },
                {"moduleTranslateIcon",@"F:\Painter3D\Resource\Texture\Icons\moduleTranslate.png"},
                {"moduleRotateIcon",@"F:\Painter3D\Resource\Texture\Icons\moduleRotate.png"},
                {"moduleScaleIcon",@"F:\Painter3D\Resource\Texture\Icons\moduleScale.png"},
            }, TextureGrid.ModeEnum.Fixed, 64);
        }
        public static void Render()
        {
            DrawMainMenu();
            DrawQuickTool();
        }

        private static void DrawQuickTool()
        {
            ImGui.Begin("Tool", ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.NoScrollbar | ImGuiWindowFlags.AlwaysAutoResize);
            ToolButton("pickIcon", () => { Tool.QuickTool.State = Tool.QuickTool.ToolStateEnum.Picker; });
            ToolButton("moveIcon", () => { });
            ToolButton("trackRotateIcon", () => { });
            ToolButton("zoomIcon", () => { });
            ToolButton("fitIcon", () => { });
            ToolButton("moduleTranslateIcon", () => { Tool.QuickTool.State = Tool.QuickTool.ToolStateEnum.Translate; });
            ToolButton("moduleRotateIcon", () => { Tool.QuickTool.State = Tool.QuickTool.ToolStateEnum.Rotate; });
            ToolButton("moduleScaleIcon", () => { Tool.QuickTool.State = Tool.QuickTool.ToolStateEnum.Scale; });
            ImGui.End();
        }

        private static void ToolButton(string name, Action toolFun = null)
        {
            if (ImGui.ImageButton(icons.Id(), new Vector2(64, 64), icons.GetTileP1(name).TK2SYS(), icons.GetTileP2(name).TK2SYS()))
            {
                Console.WriteLine("点击" + name);
                toolFun?.Invoke();
            }
        }

        private static void DrawMainMenu()
        {
            ImGui.BeginMainMenuBar();
            if (ImGui.BeginMenu("File"))
            {
                ImGui.MenuItem("LoadScence");
                ImGui.MenuItem("SaveScence");
                ImGui.MenuItem("ImportModle");
                ImGui.EndMenu();
            }
            if (ImGui.BeginMenu("Tools"))
            {
                ImGui.MenuItem("QuickTool");
                ImGui.EndMenu();
            }
            if (ImGui.BeginMenu("Scence"))
            {
                ImGui.MenuItem("General");
                ImGui.MenuItem("Graphic");
                ImGui.MenuItem("Physical");
                ImGui.MenuItem("Audio");
                ImGui.EndMenu();
            }
            if (ImGui.BeginMenu("Module"))
            {
                ImGui.MenuItem("Shape");
                ImGui.MenuItem("Joint");
                ImGui.EndMenu();
            }
            if (ImGui.BeginMenu("Analysis"))
            {
                ImGui.MenuItem("Measure");
                ImGui.MenuItem("Chart");
                ImGui.EndMenu();
            }
            ImGui.EndMainMenuBar();
        }
    }

    static class UIClassExtension
    {
        public static System.Numerics.Vector2 TK2SYS(this OpenTK.Vector2 vec)
        {
            return new System.Numerics.Vector2(vec.X, vec.Y);
        }
    }
}
